如何在sh的字符串中包含换行符? 您所在的位置:网站首页 shell 字符串追加 如何在sh的字符串中包含换行符?

如何在sh的字符串中包含换行符?

2023-09-25 07:40| 来源: 网络整理| 查看: 265

本文翻译自:How can I have a newline in a string in sh?

This 这个

STR="Hello\nWorld" echo $STR

produces as output 产生作为输出

Hello\nWorld

instead of 代替

Hello World

What should I do to have a newline in a string? 如何在字符串中包含换行符?

Note: This question is not about echo . 注意: 此问题与echo无关。 I'm aware of echo -e , but I'm looking for a solution that allows passing a string (which includes a newline) as an argument to other commands that do not have a similar option to interpret \\n 's as newlines. 我知道echo -e ,但是我正在寻找一种解决方案,该方案允许将字符串(包括换行符)作为参数传递给其他命令,这些命令没有类似的选项来将\\n解释为换行符。

#1楼

参考:https://stackoom.com/question/CbzH/如何在sh的字符串中包含换行符

#2楼

Echo is so nineties and so fraught with perils that its use should result in core dumps no less than 4GB. Echo已经九十年代了,充满了危险,以至于使用它会导致核心转储不少于4GB。 Seriously, echo's problems were the reason why the Unix Standardization process finally invented the printf utility, doing away with all the problems. 严重的是,Echo的问题是Unix标准化过程最终发明了printf实用程序的原因,它消除了所有问题。

So to get a newline in a string: 因此,要在字符串中获取换行符:

FOO="hello world" BAR=$(printf "hello\nworld\n") # Alternative; note: final newline is deleted printf '\n' "$FOO" printf '\n' "$BAR"

There! 那里! No SYSV vs BSD echo madness, everything gets neatly printed and fully portable support for C escape sequences. 没有SYSV vs BSD的回音疯狂,所有内容都被整齐地打印,并且完全支持C换码序列。 Everybody please use printf now and never look back. 大家请立即使用printf ,切勿回头。

#3楼

What I did based on the other answers was 我根据其他答案所做的是

NEWLINE=$'\n' my_var="__between eggs and bacon__" echo "spam${NEWLINE}eggs${my_var}bacon${NEWLINE}knight" # which outputs: spam eggs__between eggs and bacon__bacon knight #4楼

The problem isn't with the shell. 问题不在于外壳。 The problem is actually with the echo command itself, and the lack of double quotes around the variable interpolation. 问题实际上出在echo命令本身上,并且变量插值周围缺少双引号。 You can try using echo -e but that isn't supported on all platforms, and one of the reasons printf is now recommended for portability. 您可以尝试使用echo -e但并非所有平台都支持它,这是现在建议使用printf的原因之一,以实现可移植性。

You can also try and insert the newline directly into your shell script (if a script is what you're writing) so it looks like... 您也可以尝试将换行符直接插入到您的Shell脚本中(如果您正在编写的是脚本),这样看起来...

#!/bin/sh echo "Hello World" #EOF

or equivalently 或同等

#!/bin/sh string="Hello World" echo "$string" # note double quotes! #5楼

I find the -e flag elegant and straight forward 我发现-e标志优雅而直接

bash$ STR="Hello\nWorld" bash$ echo -e $STR Hello World

If the string is the output of another command, I just use quotes 如果字符串是另一个命令的输出,我只用引号

indexes_diff=$(git diff index.yaml) echo "$indexes_diff" #6楼

The solution is to use $'string' , for example: 解决方案是使用$'string' ,例如:

$ STR=$'Hello\nWorld' $ echo "$STR" Hello World

Here is an excerpt from the Bash manual page: 这是Bash手册页的节选:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) \b backspace \e \E an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) \cx a control-x character The expanded result is single-quoted, as if the dollar sign had not been present. A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有